package goem

import (
	"log"
	// "os"
)

func Fset0(s string, v Value) Value {
	sym := emrt.env.Intern(s)
	r := builtin_call("fset", sym, v)
	// log.Println(sym, emrt.env.NonLocalExitCheck(), r)
	return r
}

func Provide0(s string) {
	feat := emrt.env.Intern(s)
	builtin_call("provide", feat)
}
func Print0(v Value) { builtin_call("print", v) }

// TODO seem's not good
func Setq0(varname string, v Value) {
	setq := builtin_fns["setq"]
	log.Println(setq)
	sym := emrt.env.Intern(varname)
	sym = MkStr(varname)
	log.Println(sym)
	emrt.env.Funcall(setq, []Value{sym, v})
	// builtin_call("setq", MkStr(varname), v)
}
func Set0(varname string, v Value) Value {
	sym := emrt.env.Intern(varname)
	return builtin_call("set", sym, v)
}

func symbolValue(sym Value) Value {
	env := emrt.env
	symvalfn := env.Intern("symbol-value")
	rv := env.Funcall(symvalfn, []Value{sym})
	env.NonLocalExitClear()
	return rv
}

func builtin_call(name string, vs ...Value) Value {
	return emrt.env.Funcall(builtin_fns[name], vs)
}

// 比每次都都获取还是要快一些的吧
var builtin_fns = make(map[string]Value)

func builtins_init(em Module) {

	names := []string{"fset", "setq", "setq-local", "set",
		"defun", "provide", "print", "display"}
	env := em.GetEnv()
	for _, name := range names {
		builtin_fns[name] = env.Intern(name)
		// log.Println(name, builtin_fns[name])
	}

	// log.Println(MkInt(1).Type().Eq(EVTypeInt))
	for name, sym := range builtin_fns {
		switch sym.Type() {
		case EVTypeString:
			// log.Println("string", name, sym)
		case EVTypeFloat:
			// log.Println("float", name, sym)
		case EVTypeInt:
			// log.Println("int", name, sym)
		case EVTypeSymbol:
			// log.Println("symbol", name, sym)
		// case EVTypeFunc:
		// log.Println("func", name, sym)
		default:
			log.Println("unknown", name, sym)
		}
	}

	set_elisp_vars_value() // auto generated
}
